#!/usr/bin/env python
# coding=utf-8
"""
# @Created on : 2016/05/01 15:09
# @Author     : ldf (ldf@eehello.com)
# @Link       : http://www.eehello.com
# @Version    : 
# 本程序在python2.7下测试通过
"""

import SocketServer
import jc_data
import jilu
import chaxun
import datetime
import random

class my_server(SocketServer.BaseRequestHandler):

    def handle(self):
        conn_001 = self.request
        #print conn_001
        #buf = conn_001.recv(1024)
        client_addr = self.client_address[0]
        #print client_addr

        now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        #print type(now_time)
        toke001 = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        toke = toke001 + str(random.randint(101,999))

        #huanying = '你好，我是小e。请问有什么能为您效劳的吗？如果你不想和我聊天了，请输入exit'
        huanying = 'hello! my name is ee'
        conn_001.sendall(huanying)

        jilu.jilu(toke,now_time,huanying,'server')

        flag = 1
        while flag:
            client_data_001 = conn_001.recv(1024)

            now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

            jilu.jilu(toke,now_time,client_data_001,client_addr)
            #print client_data_001

            if client_data_001 == 'exit':
                flag = 0
                continue
            if client_data_001 == 'chaxun':
                #conn_001.sendall('-------------------------------')
                chaxun01 = chaxun.run(toke)
                conn_001.sendall(chaxun01)
                #conn_001.sendall('-------------------------------')

            else:
                server_data_001 = jc_data.jc_data(client_data_001)
                conn_001.sendall(server_data_001)

                jilu.jilu(toke,now_time,server_data_001,'server')
                #print server_data_001


        conn_001.close()


def server():
    ip_port = ('127.0.0.1',8888)

    sk = SocketServer.ThreadingTCPServer(ip_port,my_server)
    sk.serve_forever()
